import { ConfigManager } from "../resource/ConfigManager";
import { AttributeMergerInfo } from "./AttributeMergerInfo";
import { MergerTypeConst } from "./MergerTypeConst";
//class JsonMergerConst
    
    export  class JsonMergerConst
    {
        constructor()
        {
            
            this.mergerDic = new Map<any, any> /* flash.utils.Dictionary */();
            this._unBindObjDic = new Map<any, any> /* flash.utils.Dictionary */();
            if (JsonMergerConst._instance) 
            {
                throw new Error("JsonFilterConst 单例");
            }
            this.readMergerConfig();
            return;
        }

        /* internal  */readMergerConfig(): void
        {
            var loc2=undefined;
            var loc3=null;
            var loc1=ConfigManager.instance.getObjectByFileName(JsonMergerConst.MergerFileName);
            if (loc1 && loc1["mergers"]) 
            {
                loc2 = loc1["mergers"]["merger"];
                if (typeof loc2 === "array") 
                {
                    var loc4=0;
                    var loc5=loc2;
                    for(loc3 of loc5) 
                    {
                        this.mergerDic[loc3["fileName"]] = this.getInfo(loc3);
                    }
                }
                else if (typeof loc2 === "object") 
                {
                    this.mergerDic[loc2["fileName"]] = this.getInfo(loc2);
                }
            }
            return;
        }

        /* internal  */getInfo(arg1: Object): AttributeMergerInfo
        {
            return new AttributeMergerInfo(arg1["fileName"], arg1["keyCode"], arg1["mergerType"], arg1["jsonDifferentKeyName"]);
        }

        public revertAttributes(arg1: string, arg2: Object): Object
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=this._unBindObjDic;
            for (loc1 in loc6) 
            {
                delete this._unBindObjDic[loc1];
            }
            if (loc2 = this.mergerDic[arg1]) 
            {
                if (typeof arg2 === "array") 
                {
                    loc3 = [];
                    loc5 = 0;
                    loc6 = arg2;
                    for(loc4 of loc6) 
                    {
                        loc3.push(this.revertObjectImpl(arg1, loc2, loc4));
                    }
                    return loc3;
                }
                if (typeof arg2 === "object") 
                {
                    return this.revertObjectImpl(arg1, loc2, arg2);
                }
            }
            return arg2;
        }

        /* internal  */revertObjectImpl(arg1: string, arg2: AttributeMergerInfo, arg3: Object): Object
        {
            var loc2=0;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            if (arg2.attrKeyCode == "") 
            {
                return arg3;
            }
            var loc1=arg3;
            if (arg2.attrKeyCode != "") 
            {
                loc2 = int(arg3[arg2.attrKeyCode]);
                if (arg2.mergerType != MergerTypeConst.Type_Item) 
                {
                    if (arg2.mergerType != MergerTypeConst.Type_Skill) 
                    {
                        return arg3;
                    }
                    else 
                    {
                        loc2 = int(loc2 / 100) * 100;
                    }
                }
                else 
                {
                    loc2 = int(loc2 / 10) * 10;
                }
                loc3 = arg1 + loc2.toString();
                if (this._unBindObjDic[loc3]) 
                {
                    if (arg3[arg2.jsonDifferentKeyName]) 
                    {
                        loc4 = arg3[arg2.jsonDifferentKeyName] as Array<any>;
                    }
                    loc5 = this._unBindObjDic[loc3];
                    var loc7=0;
                    var loc8=loc5;
                    for (loc6 in loc8) 
                    {
                        if (!(!arg3.hasOwnProperty(loc6) && (!loc4 || loc4.indexOf(loc6) == -1))) 
                        {
                            continue;
                        }
                        loc1[loc6] = loc5[loc6];
                    }
                }
                else 
                {
                    this._unBindObjDic[loc3] = arg3;
                    return arg3;
                }
            }
            return loc1;
        }

        public static get instance(): JsonMergerConst
        {
            if (!JsonMergerConst._instance) 
            {
                JsonMergerConst._instance = new JsonMergerConst();
            }
            return JsonMergerConst._instance;
        }

        public static /* const */MergerFileName: string="JsonMergerConfig.xml";

        private /* var */mergerDic: Map<any, any> /* flash.utils.Dictionary */;

        private /* var */_unBindObjDic: Map<any, any> /* flash.utils.Dictionary */;

        private static /* var */_instance: JsonMergerConst;
    }
